import i18n from "i18next";
import {initReactI18next} from "react-i18next";
import HttpApi from "i18next-http-backend";
import LanguageDetector from 'i18next-browser-languagedetector';
import {configChange} from "@core/Config";
import {getItem, setItem} from "@core/services/LocalDataService";
import {formatStr} from "@core/utils";

import zh_ant from "antd/locale/zh_CN";
import zh_local from "./locale/zh.json";

import en_ant from "antd/locale/en_US";
import en_local from "./locale/en.json";

import pt_ant from "antd/locale/pt_PT";
import pt_local from "./locale/pt.json";


let localLang: string;

configChange((Config: any) => {
    localLang = Config.localLang;
});

const defaultLanguage: string[] = ["zh", "en", "pt"];

const languageMap = {
    zh: {
        translation: {
            ...zh_ant, ...zh_local
        }
    },
    en: {
        translation: {
            ...en_ant, ...en_local
        }
    },
    pt: {
        translation: {
            ...pt_ant, ...pt_local
        }
    }
};

i18n.use(LanguageDetector)
    .use(HttpApi)
    .use(initReactI18next)
    .init({
        resources: languageMap,
        fallbackLng: defaultLanguage,
        debug: false,
        react: {
            useSuspense: false
        },
        interpolation: {
            escapeValue: false
        }
    });

const changeLanguage: Function = (key: string): void => {
    i18n.changeLanguage(key);
    setItem(localLang, key);
    window.location.reload()
};

// @ts-ignore
const getLanguage: Function = (): any => {
    let cur: string = getI18n() || defaultLanguage;
    return languageMap[cur].translation || {}
};

// @ts-ignore
const getLanguageById: Function = (field: string, obj: any) => {
    let str: string = getLanguage()[field] || "";
    return obj ? formatStr(str, obj) : str;
};
// @ts-ignore
const getLanguageByKey: Function = (field: string) => {
    let i18nData: any = i18n.toJSON().store.data;
    if (!i18nData.zh) {
        return {
            zh: "管理系统",
            en: "Management System",
            pt: "葡萄牙管理系统"
        }
    }
    return {
        zh: i18nData.zh.translation[field] || "管理系统",
        en: i18nData.en.translation[field] || "Management System",
        pt: i18nData.pt.translation[field] || "葡萄牙管理系统"
    }
};

// @ts-ignore
const getLocale: Function = (zh: string, en: string, pt: string) => {
    const cur: string = getI18n() || defaultLanguage;
    switch (cur) {
        case "zh":
            return zh;
        case "en":
            return en;
        case "pt":
            return pt;
    }
};

// @ts-ignore
const getI18n: Function = () => {
    const cur: string = getItem(localLang) || defaultLanguage;
    let currentLng = "zh";
    switch (cur) {
        case "en":
        case "en_US":
            currentLng = "en";
            break;
        case "zh":
        case "zh-CN":
            currentLng = "zh";
            break;
        case "pt":
        case "pt-PT":
            currentLng = "pt";
            break;
        default:
            currentLng = "zh";
            break;
    }
    return currentLng;
};

export {changeLanguage, getLanguage, getLanguageById, getLanguageByKey, getLocale, getI18n, defaultLanguage};

export default i18n;
